﻿using System;
using System.Configuration;

namespace NDBGen.Configuration
{
    public class SchemaDataAccessOutputElement : ProjectManagedEntryConfigurationElement, ICollectionItemElement
    {
        const String PROPNAME_GENERATOR = "generator";
        [ConfigurationProperty(PROPNAME_GENERATOR, IsKey = true, IsRequired = true)]
        public String Generator
        {
            get { return (String)this[PROPNAME_GENERATOR]; }
            set { this[PROPNAME_GENERATOR] = value; }
        }

        const String PROPNAME_MODELANDCONTEXTINSAMEFILE = "modelAndContextInSameFile";
        [ConfigurationProperty(PROPNAME_MODELANDCONTEXTINSAMEFILE, IsKey = true, IsRequired = false, DefaultValue = false)]
        public Boolean ModelAndContextInSameFile
        {
            get { return (Boolean)this[PROPNAME_MODELANDCONTEXTINSAMEFILE]; }
            set { this[PROPNAME_MODELANDCONTEXTINSAMEFILE] = value; }
        }

        const String PROPNAME_GENERATEBASECONTEXT = "generateBaseContext";
        [ConfigurationProperty(PROPNAME_GENERATEBASECONTEXT, DefaultValue = true)]
        public Boolean GenerateBaseContext
        {
            get { return (Boolean)this[PROPNAME_GENERATEBASECONTEXT]; }
            set { this[PROPNAME_GENERATEBASECONTEXT] = value; }
        }

        const String PROPNAME_SETTINGS = "settings";
        // language
        [ConfigurationCollection(typeof(SettingElement))]
        [ConfigurationProperty(PROPNAME_SETTINGS, IsDefaultCollection = true)]
        public GenericElementCollection<SettingElement> Settings
        {
            get { return (GenericElementCollection<SettingElement>)this[PROPNAME_SETTINGS]; }
            set { this[PROPNAME_SETTINGS] = value; }
        }

        public String GetSetting(String name)
        {
            var setting = this.Settings.Get(name);
            if (setting == null) return null;
            else return setting.Value;
        }

        #region [ IConfigurationElement Members ]

        const String NOMANAGER = "[no manager]";
        const String NOPROJECTFILE = "[no project file]";
        Object ICollectionItemElement.Key
        {
            get { return String.Format("{0}{1}->{2}", this.Generator, this.ProjectFileManager ?? NOMANAGER, this.ProjectFile ?? this.ProjectRoot); }
        }

        #endregion
    }
}